//
//  HomeViewController.m
//  1243
//
//  Created by 我的 on 16/3/22.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "HomeViewController.h"
#import "TableViewCellHeader.h"
#import "ShouJiChongZhiViewController.h"
#import "ZhuanZhangViewController.h"
#import "Masonry.h"
#import "AFNetworking.h"
#import "NetWorkTools.h"
#import "UIDevice+DeviceType.h"
#import "SweepViewController.h"
#import "ErWeiMaViewController.h"
#import "JudgeUserLogInTool.h"
#import "RegisterFirstViewController.h"
#import "LoginViewController.h"
#import "ReadLoginData.h"
#import "XRCarouselView.h"
#import "ShoeBagDressViewController.h"
@interface HomeViewController ()<XRCarouselViewDelegate>

@property (nonatomic,strong) NSArray *imageViewArray;

@property (nonatomic, strong) XRCarouselView *carouselView;
@end

@implementation HomeViewController
- (NSArray *)imageViewArray {
    if (!_imageViewArray) {
        _imageViewArray = @[
                            [UIImage imageNamed:@"beidahuang"],
                            [UIImage imageNamed:@"Home_39"],
                            [UIImage imageNamed:@"Home_41"]
                            ];
    }
    return _imageViewArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setHeaderView];
    
    [self setNavBarItem];
    
//    NSLog(@"%@",[self userText]);
#warning 接口测试
//    [self loadUserDataTest];
//    [self logIn];
    
//    NSLog(@"%@",[self getSha1String:@"ADDUSERabc_1qazxsw@"]);
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 180)];
    headerView.backgroundColor = [UIColor whiteColor];
    [self addHeaderView:headerView];
    [self.view addSubview:headerView];
    
}

- (void)addHeaderView:(UIView *)headerView {
    
    NSArray *images = @[
                        [UIImage imageNamed:@"Home_37"],
                        [UIImage imageNamed:@"beidahuang"],
                        [UIImage imageNamed:@"topbanner_1"],
                        [UIImage imageNamed:@"topbanner_2"],
                        [UIImage imageNamed:@"topbanner_3"]
                        ];
    
    
    /**
     *  通过代码创建
     */
    self.carouselView = [XRCarouselView carouselViewWithImageArray:images describeArray:nil];
    
    //设置frame
    self.carouselView.frame = CGRectMake(0, 0, WIDTH, 180);
    
    //用代理处理图片点击，如果两个都实现，block优先级高于代理
    self.carouselView.delegate = self;
    
    [self.carouselView setPageColor:[UIColor whiteColor] andCurrentPageColor:[UIColor blueColor]];
    
    //设置每张图片的停留时间
    _carouselView.time = 3;
    
    [headerView addSubview:_carouselView];
    
}

#pragma mark XRCarouselViewDelegate
- (void)carouselView:(XRCarouselView *)carouselView didClickImage:(NSInteger)index {
    NSLog(@"点击了第%ld张图片", index);
    if (index == 1) {
        [self pushForm:self.navigationController pushToAndHidden:[[ShoeBagDressViewController alloc] init]];
    }
#warning -----------轮播图点击事件
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    
    NSDictionary *dict  = [ReadLoginData readLoginData];
    
    if (dict.count) {
        // 已登录
        
    }else{
        LoginViewController *a = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
        [self presentViewController:a animated:YES completion:^{
            
        }];
        
    }
    
    
    
}

- (NSDictionary *)userText {

    NSString *string = @"{\"Number\":1,\"UserName\":\"13889464583\",\"Password\":\"123456\",\"Buy_Password\":\"123\",\"RealName\":\"test\",\"Email\":\"text@163.com\",\"V_Number\":1,\"V_Name\":\"vname_test\",\"LastLoginTime\":\"2016-04-12T15:47:20.6938427+08:00\",\"PNumber1\":1,\"PName1\":\"test1\",\"PNumber2\":2,\"PName2\":\"test2\",\"PNumber3\":3,\"PName3\":\"test3\",\"UserRole\":\"RoleTest\",\"Money\":0.0,\"fh_Money\":0.0,\"gq_Money\":0.0,\"gwj_Money\":0.0,\"Dl_Money\":0.0,\"Status\":0,\"CreateTime\":\"2016-04-12T15:47:20.6928426+08:00\",\"Remark\":\"测试\"}";
    
    return [NetWorkTools dictionaryWithJsonString:string];
}

- (void)logIn {
    
    /*
        [(null)] - 请求日志:{"MenthodName":"CREATEORDER","MenthodParms":"{\"UserId\":\"1\",\"UserName\":\"18906539950\",\"ProductId\":\"40476\",\"BuyNum\":\"1\",\"ReceiveName\":\"麦西\",\"ReceiveMobile\":\"18906539950\",\"ReceivePhone\":\"\",\"ReveiveProvice\":\"\",\"ReceiveCity\":\"杭州\",\"ReceiveAddress\":\"浙江省杭州市文二路节能公司\"}","Sign":"D979BAD25BE42D2FD4E963402D4177DF9855CDF9"}
     */
    
    NSDictionary *dict = @{
                           @"MerchantId":@"1",
                           @"Sort":@"",
                           @"Type":@"1",
//                           @"FromPassword":@"654321",
                           };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"BANNERQUERY" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        
//        NSLog(@"%@",[NetWorkTools arrayWithJsonString:[object objectForKey:@"BackData"]]);
//        
        NSArray *array = [NetWorkTools arrayWithJsonString:[object objectForKey:@"BackData"]];
        NSLog(@"%@",array);
        
    }];
    
}




- (void)loadUserDataTest {
    
    NSDictionary *dict = @{
                           @"Buy_Password" :@"123",
                           @"CreateTime" : @"2016-04-12T15:47:20",
                           @"Dl_Money" : @"0",
                           @"Email" : @"text@163.com",
                           @"LastLoginTime" : @"2016-04-12T15:47:20",
                           @"Money" : @"0",
                           @"Number" : @"1",
                           @"PName1" : @"test1",
                           @"PName2" : @"test2",
                           @"PName3" : @"test3",
                           @"PNumber1" : @"1",
                           @"PNumber2" : @"2",
                           @"PNumber3" : @"3",
                           @"Password" : @"123456",
                           @"RealName" : @"test",
                           @"Remark" : @"iOS",
                           @"Status": @"0",
                           @"UserName" : @"13813881338",
                           @"UserRole" : @"RoleTest",
                           @"V_Name" : @"vname_test",
                           @"V_Number" : @"1",
                           @"fh_Money" : @"0",
                           @"gq_Money" : @"0",
                           @"gwj_Money" : @"0"
                           };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"ADDUSER" completeBlock:^(id object) {
        NSLog(@"%@",object);
    }];
}


- (void)setNavBarItem {
    //// 扫一扫
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"nav_1"] highImage:[UIImage imageNamed:@"nav_1"] target:self action:@selector(saoYiSaoBtn) imageEdgeInsets:UIEdgeInsetsMake(0, 10, 15, 0) titleEdgeInsets:UIEdgeInsetsMake(20, -25, 0, 25) title:@"扫一扫"];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"nav_3"] highImage:[UIImage imageNamed:@"nav_3"] target:self action:@selector(fuKuanBtn) imageEdgeInsets:UIEdgeInsetsMake(0, 35, 15, -35) titleEdgeInsets:UIEdgeInsetsMake(20, 20, 0, 0) title:@"收款"];
    
//    NSString *strModel = [[UIDevice currentDevice] machine];
    
    CGRect frame;
    if (WIDTH == 320) {
        frame = CGRectMake(-25, 0, 200, 30);
    } else {
        frame = CGRectMake(0, 0, 200, 30);
    }
    UIView *view = [[UIView alloc] initWithFrame:frame];

    UITextField *searchFeild = [[UITextField alloc] initWithFrame:frame];
    searchFeild.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入商家或者商品名称" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    searchFeild.textColor = [UIColor redColor];
    searchFeild.font = [UIFont systemFontOfSize:14];
    searchFeild.layer.cornerRadius = searchFeild.frame.size.height/2;
    searchFeild.layer.masksToBounds = YES;
    searchFeild.enabled = NO;
    searchFeild.layer.borderWidth = 1;
    searchFeild.layer.borderColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1].CGColor;
    [self setLeftViewWithTextField:searchFeild imageName:@"nav_2"];
    [view addSubview:searchFeild];

    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = searchFeild.frame;
    [searchBtn addTarget:self action:@selector(searchBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:searchBtn];
    
    self.navigationItem.titleView = view;
}

- (void)searchBut {
    NSLog(@"...");
}

- (void)saoYiSaoBtn {
    if ([JudgeUserLogInTool  JudgeUserLonInStatus]) {
    SweepViewController *a = [[SweepViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:a];}
}

- (void)fuKuanBtn {
    if ([JudgeUserLogInTool  JudgeUserLonInStatus]) {
    ErWeiMaViewController *a = [[ErWeiMaViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:a];}
}

//tabBar 隐藏
- (void)pushForm:(UINavigationController *)pushForm pushToAndHidden:(UIViewController *)pushToVC
{
    [pushToVC setHidesBottomBarWhenPushed:YES];
    [pushForm pushViewController:pushToVC animated:YES];
    
}

/**
 *  给UITextField设置右侧的图片
 *
 *  @param textField UITextField
 *  @param imageName 图片名称
 */

-(void)setLeftViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    
    rightView.image = [UIImage imageNamed:imageName];
    rightView.frame = CGRectMake(0, 0, CGRectGetHeight(textField.frame), CGRectGetHeight(textField.frame));
    rightView.contentMode = UIViewContentModeCenter;
    
    textField.leftView = rightView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.backgroundColor = [UIColor blackColor];
    textField.alpha = 0.25;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.imageViewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, WEITH - 10, 130)];
    imageView.layer.cornerRadius = 10;
    imageView.layer.masksToBounds = YES;
    imageView.image = self.imageViewArray[indexPath.row];
    cell.contentView.backgroundColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:imageView];
    
    return cell;
}

#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        [self pushForm:self.navigationController pushToAndHidden:[[ShoeBagDressViewController alloc] init]];
    }
}

- (void)setHeaderView {
    
    TableViewCellHeader *headerView = [TableViewCellHeader loadTableViewCellHeader];
    
    [headerView.shouJiChongZhiBtn addTarget:self action:@selector(didClickShouJiButton) forControlEvents:UIControlEventTouchUpInside];
    
    [headerView.zhuanZhangBtn addTarget:self action:@selector(didClickZhuanZhangBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [headerView.sheQuFuWuBtn addTarget:self action:@selector(didClickShangChengButtonNotification) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.tableHeaderView = headerView;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = 140;

}

#pragma mark 点击按钮事件
- (void)didClickShangChengButtonNotification {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DidClickButtonChangeTabBarControllerSelected" object:nil userInfo:nil];
}

- (void)didClickShouJiButton {
    
    NSLog(@"...");
    
//    NSLog(@"%s",__func__);
    ShouJiChongZhiViewController *chongZhi = [[ShouJiChongZhiViewController alloc] init];
    [self.navigationController pushViewController:chongZhi animated:YES];
}

- (void)didClickZhuanZhangBtn {
    ZhuanZhangViewController *zhuanZhangVC = [[ZhuanZhangViewController alloc] init];
    [self.navigationController pushViewController:zhuanZhangVC animated:YES];
}

@end
